package com.dajingling.wx.filter;

import com.dajingling.bean.UserAdmin;
import com.dajingling.util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("*.next")
public class AccessControlFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("-------过滤器启动----");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        /*
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestURI = request.getRequestURI();
        UserAdmin user = UserUtil.getWxUser(request.getSession());
        if (user==null){
            //先判断访问什么，再决定（放行名单）
            if (requestURI.endsWith("/") || requestURI.endsWith("wxloginpwd.next") || requestURI.endsWith("wxlogin.next") || requestURI.endsWith("login.html") || requestURI.endsWith("/wxloginSms.next")){
                System.out.println("微信用户正在访问："+requestURI+",已允许");
                filterChain.doFilter(servletRequest, servletResponse);
            }else {
                System.out.println("微信用户正在访问："+requestURI+",权限不足");
                response.sendRedirect("login.html");
                //response.sendError(404,"很遗憾权限不足");
            }
        }else {
            //放行
            System.out.print("微信用户："+user.getUserPhone()+",正在访问："+requestURI+",已允许");
            filterChain.doFilter(servletRequest, servletResponse);
        }

         */
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("----过滤器关闭-----");
    }
}
